package com.cnfangmao.masterdata.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cnfangmao.masterdata.dao.FieldRuleDao;
import com.cnfangmao.masterdata.dto.FieldRuleSearchDto;
import com.cnfangmao.masterdata.dto.GeneralSearchDto;
import com.cnfangmao.masterdata.model.FieldRule;
import com.cnfangmao.masterdata.service.FieldRuleService;

/**
 * <p>
 * 字段规则关联表 服务实现类
 * </p>
 *
 * @author chenxd
 * @since 2020-04-14
 */
@Service
public class FieldRuleServiceImpl extends ServiceImpl<FieldRuleDao, FieldRule> implements FieldRuleService {
	
	@Autowired
	private FieldRuleDao dao;
	
	@Override
	public FieldRule findByRuleIdAndTargetId(String ruleId, String targetId) {
		FieldRule fieldRule =null;
		QueryWrapper<FieldRule> queryWrapper =new QueryWrapper<FieldRule>();
		queryWrapper.eq("rule_id", ruleId);
		queryWrapper.eq("target_id", targetId);
		fieldRule=dao.selectOne(queryWrapper);
		return fieldRule;
	}

	@Override
	public IPage<FieldRule> queryListFieldRule(GeneralSearchDto<FieldRuleSearchDto> dto) {
		IPage<FieldRule> page = dao.selectPage(new Page<>(dto.getPageNo(),dto.getPageSize()), new QueryWrapper<FieldRule>()
				.like(StringUtils.isNotEmpty(dto.getConditions().getRuleId()), "rule_id", dto.getConditions().getRuleId())
				.or().like(StringUtils.isNotEmpty(dto.getConditions().getTargetId()), "target_id", dto.getConditions().getTargetId())
				);
		return page;
	}

}
